package com.slicktechnologies.shared.common.businessunitlayer;

import com.google.gwt.core.shared.GwtIncompatible;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Ignore;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.OnLoad;
import com.googlecode.objectify.annotation.OnSave;
import com.simplesoftwares.client.library.helperconstants.FormTypes;
import com.simplesoftwares.rebind.annatonations.TableAnnotation;
import com.slicktechnologies.shared.MyUtility;
import com.slicktechnologies.shared.common.helperlayer.PersonInfo;
import com.slicktechnologies.shared.common.relationalLayer.BranchRelation;

import static com.googlecode.objectify.ObjectifyService.ofy;

/**
 * Repersents A Branch.
 */
@Entity
@Cache
public class Branch extends BusinessUnit  
{	
	/***********************************************Entity Attributes****************************************************/
	/**The Constant serialVersionUID. */
	private static final long serialVersionUID = 982013130256104959L;
	
	/**The status of this branch can be active or inactive */
	protected boolean status;
	
	
	/***********************************************Join Kind Attribute*****************************************************/
	@Index
	protected Key<BranchRelation> keyName;
	@Index
	protected String calendarName;
	protected boolean nameChanged=false;
	
	

	/*******************************************Default Ctor********************************************************/
	/**
	 * Instantiates a new branch.
	 */
	public Branch() 
	{
		super();
		nameChanged=false;	
		calendarName="";
	}
	
	/**
	 * Gets the status.
	 * @return the status
	 */
	@TableAnnotation(ColType = FormTypes.TEXTCOLUM, colNo = 5, isFieldUpdater = false, isSortable = true, title = "Status")
	public Boolean getstatus() 
	{
		return status;
	}

	/**
	 * Sets the status.
	 * @param status the new status
	 */
	public void setstatus(boolean status) 
	{
		this.status=status;	
	}

	@Override
	public void setBusinessUnitName(String buisnessunitName) 
	{
		
		if(buisnessunitName!=null)
		{
		System.out.println("Boolean Condition is "+buisnessUnitName.equals(buisnessunitName.trim()));
		if(!(this.buisnessUnitName.equals(buisnessunitName.trim())))
			nameChanged=true;
		this.buisnessUnitName=buisnessunitName;
		}
	}

	/**************************************************Relation Managment Part**********************************************/
	@OnSave
	@GwtIncompatible
	public void onSave()
	{
		if(id==null)
		{
			BranchRelation branchRelation=new BranchRelation();
			branchRelation.setName(buisnessUnitName);
			
			keyName=(Key<BranchRelation>) MyUtility.createRelationalKey(branchRelation);
			System.out.println("kEY NAME "+keyName);
		}
		else
		{
			
			if(nameChanged==true)
			{
				if(keyName!=null)
				{
					BranchRelation branchRelation=new BranchRelation();
					branchRelation.setName(buisnessUnitName);
					MyUtility.updateRelationalEntity(branchRelation,keyName);
					System.out.println("iii");
				}
			}
		}
		
		//To handle Batch Assignments
		
		
	}
	
	
	@OnLoad
	@GwtIncompatible
	public void onLoad()
	{
		
		
	}

	

	public String getCalendarName() {
		return calendarName;
	}

	public void setCalendarName(String calendarName) {
		this.calendarName = calendarName;
	}
	
	
	
	/***********************************************************************************************************************/
}
